package com.zenithsun.news.specialnews.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.syzg.common.service.impl.BaseServiceImpl;
import com.syzg.core.exception.ServiceException;
import com.syzg.core.page.DefaultPageResult;
import com.syzg.core.page.PageResult;
import com.syzg.core.util.StringUtil;
import com.zenithsun.news.specialnews.service.SpecialnewsService;
import com.zenithsun.news.specialnews.dao.SpecialnewsDAO;
import com.zenithsun.news.specialnews.model.Specialnews;

@Service("specialnewsService")
public class SpecialnewsServiceImpl extends BaseServiceImpl<Specialnews> implements SpecialnewsService {
    
    private SpecialnewsDAO specialnewsDAO = null;

    @Resource
	public void setSpecialnewsDAO(SpecialnewsDAO specialnewsDAO) {
		this.setDAO(specialnewsDAO);
		this.specialnewsDAO = specialnewsDAO;
	}

	@Override
	public void createSpecialNews(Specialnews specialnews, String selectedNews) throws ServiceException{
		this.create(specialnews);
		if(StringUtil.isNotEmpty(selectedNews)){
			String[] newsIds = selectedNews.split(",");
			specialnewsDAO.updateNews(specialnews.getId(), newsIds);
		}
	}

	@Override
	public void updateSpecialNews(Specialnews specialnews, String selectedNews) throws ServiceException {
		this.update(specialnews);
		if(StringUtil.isNotEmpty(selectedNews)){
			String[] newsIds = selectedNews.split(",");
			specialnewsDAO.updateNews4Edit(specialnews.getId(),newsIds);
		}
	}
	
	/**
	 * 搜索功能
	 * @param pageNo
	 * @param limit
	 * @param sn
	 * @return
	 */
	@Override
	public PageResult findObjectsByList(int pageNo, int limit, Specialnews sn)throws Exception{
		int totalCount = this.specialnewsDAO.getSpecialNewsCount(sn);
    	List items = this.specialnewsDAO.findObjectsList(pageNo, limit,sn);
		PageResult pageResult = new DefaultPageResult(items, totalCount, (pageNo - 1) * limit, limit);
		return pageResult;
	}
}
